package android.itv.library;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.itv.model.BroadcastSchedule;
import android.itv.model.Channel;
import android.itv.model.Notification;
import android.util.Log;

/**
 * Class nay chua cac phuong thuc lam viec voi DB
 * @author XUANTUNG
 *
 */
public class Database
{
    private  Context context;
    
    private SQLiteDatabase sqlDB;
    
    private  String pathDB="";
    private  String pathDirDB="";
    private final String NAMEDB="itv";
    
    private final String TABLE_BROADCASTSCHEDULE="broadcastschedule";
    private final String TABLE_BROADCASTSCHEDULE_IDCHANNEL="idchannel";
    private final String TABLE_BROADCASTSCHEDULE_DATE="date";
    private final String TABLE_BROADCASTSCHEDULE_LISTPROGRAME="listprograme";
    
    private final String TABLE_CHANNEL="channel";
    private final String TABLE_CHANNEL_NAME="name";
    private final String TABLE_CHANNEL_IDCHANNEL="idchannel";
    private final String TABLE_CHANNEL_LINKLOGO="linklogo";
    private final String TABLE_CHANNEL_LINKTIVI="linktivi";
    private final String TABLE_CHANNEL_DESCRIPTION="descriptions";
    private final String TABLE_CHANNEL_CATEGORY="category";
    
    
    public Database(Context context)
    {	
	this.context=context;
	pathDB="/data/data/"+context.getPackageName()+"/databases/itv";
	pathDirDB="/data/data/"+context.getPackageName()+"/databases";
    }
    
    public void open()
    {
	sqlDB=SQLiteDatabase.openDatabase(pathDB, null, SQLiteDatabase.OPEN_READWRITE);
    }
    
    public void close()
    {
	sqlDB.close();
    }
    
    public  void moveDB()
    {
	File file=new File(pathDB);
	if(!file.exists())
	{
	    File f=new File(pathDirDB);
	    f.mkdir();
	    try
	        {
		    InputStream is=context.getAssets().open(NAMEDB);
		    FileOutputStream fos=new FileOutputStream(pathDB);
		    byte[] b=new byte[1024];
		    int length;
		    while((length=is.read(b))>0)
		    {
			fos.write(b, 0, length);		
		    }
		    is.close();
		    fos.close();
	        }
	        catch (IOException e)
	        {
		    e.printStackTrace();
	        }
	}	
    }
    
    public void saveBroadcastSchedule(String idchannel,String date,String listprograms)
    {
	ContentValues contentV=new ContentValues();
	contentV.put(TABLE_BROADCASTSCHEDULE_IDCHANNEL, idchannel);
	contentV.put(TABLE_BROADCASTSCHEDULE_DATE, date);
	contentV.put(TABLE_BROADCASTSCHEDULE_LISTPROGRAME, listprograms);
	open();
	sqlDB.insert(TABLE_BROADCASTSCHEDULE,null, contentV);
	close();
    }
    
    public void saveChannels(ArrayList<Channel> listChannel)
    {
	ContentValues contentV;
	open();
	for(int i=0;i<listChannel.size();i++)
	{
	    	contentV=new ContentValues();
	        contentV.put(TABLE_CHANNEL_NAME, listChannel.get(i).getName());
	        contentV.put(TABLE_CHANNEL_IDCHANNEL, listChannel.get(i).getIdChannel());
	        contentV.put(TABLE_CHANNEL_CATEGORY, listChannel.get(i).getCategory());
	        contentV.put(TABLE_CHANNEL_DESCRIPTION, listChannel.get(i).getDescriptions());
	        contentV.put(TABLE_CHANNEL_LINKLOGO, listChannel.get(i).getLinkLogo());
	        sqlDB.insert(TABLE_CHANNEL, null, contentV);
	}
	close();
    }
    
    public BroadcastSchedule  loadBroadcastSchedule(String idchannel,String date)
    {
	String sql="select "+ TABLE_BROADCASTSCHEDULE_LISTPROGRAME + " from "+ TABLE_BROADCASTSCHEDULE+" where "+TABLE_BROADCASTSCHEDULE_IDCHANNEL+" =? and "+TABLE_BROADCASTSCHEDULE_DATE+"=?";
	String listprograme="";
	BroadcastSchedule broadcast=new BroadcastSchedule();
	String[] arg={idchannel,date};
	open();
	Cursor c=sqlDB.rawQuery(sql,arg);
		try
		{
        		c.moveToFirst();
        		listprograme=c.getString(0);
        		broadcast.setIdChannel(idchannel);
        		broadcast.setDate(date);
        		broadcast.setListprograme(JsonPrase.parseSchedule(listprograme));
        		c.close();
		}
		catch (Exception e) 
		{
		    return broadcast; 
		}
        	finally
        	{
        	    close();        	          	    
        	}
	return broadcast;  
    }
    
    public String getNameChannel(String idChannel)
    {
	String sql="select name from channel where idchannel=?";
	String name="";
	String[] arg={idChannel};
	open();
	Cursor c=sqlDB.rawQuery(sql, arg);
	try
	{
		c.moveToFirst();
		name=c.getString(0);		
		c.close();
	}
	catch (Exception e) 
	{
	    return name;
	}
	finally
	{
	    close();	    
	}
	return name;
    }
 
    public void saveNotification(Notification notification)
    {
	String nameChannel=notification.getNameChannel();
	String time=notification.getTime();
	String content=notification.getContent();
	ContentValues values=new ContentValues();
	values.put("namechannel",nameChannel);
	values.put("time",time);
	values.put("content",content);
	open();
	sqlDB.insert("notifications", null, values);
	close();
    }
    
    public ArrayList<Notification> getNotifications()
    {
	String sql="select namechannel,time,content from notifications";
	ArrayList<Notification> listNotification=new ArrayList<Notification>();
	String nameChannel="";
	String time="";
	String content="";	
	
	open();
	Cursor c=sqlDB.rawQuery(sql, null);
	if(c!=null)
	{
	    while(c.moveToNext())
	    {
		nameChannel=c.getString(0);
		time=c.getString(1);
		content=c.getString(2);
		Notification noti=new Notification(nameChannel, time, content);
		listNotification.add(noti);
	    }
	    c.close();
	}
	close();
	return listNotification;
    }
    
    public void deleteNotifications(String content)
    {
	Log.d("content",content);
	String[] arg={content};
	open();
	sqlDB.delete("notifications","content=?", arg);
	close();
    }
    
    public int countNotifications()
    {
	int count=0;
	open();
	//sqlDB.query("notifications", "content", null, null, null, null);
	Cursor s=sqlDB.query("notifications",null,null,null, null, null, null);
	if(s!=null)
	count=s.getCount();
	close();
	return count;
    }
}
